package com.xinyue.user.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.xinyue.user.dao.NewsMapper;
import com.xinyue.user.pojo.Comment;
import com.xinyue.user.pojo.News;
import com.xinyue.user.pojo.User;
import com.xinyue.user.service.Impl.CommentServiceImpl;
import com.xinyue.user.service.Impl.NewsServiceImpl;
import com.xinyue.user.service.Impl.UserServiceImpl;
import com.xinyue.user.utils.FileRenameUtil;

@RestController
public class NewsController {

	@Autowired
	private NewsServiceImpl newsService;
	
	@Autowired
	private UserServiceImpl userService;
	
	@Autowired
	private CommentServiceImpl commentService;
	
	
	//发布动态
	@PostMapping("/insertNews")
	public String insertNews(HttpSession session,@RequestParam("pic") MultipartFile pic,News n) {
		try {
			/*
			 * User user = (User)session.getAttribute("username"); 
			 * n.setUid(user.getId());
			 */
			n.setUid(1);
			//存储照片
			String picture = FileRenameUtil.save(pic);
			n.setPicture(picture);
			//设置发送信息的时间
			java.sql.Date time= new java.sql.Date(new Date().getTime());
			n.setDate(time);
			newsService.insert(n);
			return "ok";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	//点赞动态，该动态的点赞数+1
	@GetMapping("/updateLikes")
	public String updateLikes(Integer id) {
		try {
			newsService.updateLikes(id);
			return "ok";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
	}
	
	/**
	 * 查看动态，每次返回八条动态
	 * @param page  页数，一次查询八条记录
	 * @param status   1代表全部  2代表  附近  3代表  男的动态  4  代表女的动态
	 * @return
	 */
	@GetMapping("/showNews")
	public List<News> showNews(Integer page,Integer status,String city){
		System.out.println(page);
		List<News> news= null;
		int start = (page-1)*8;
		try {
			if(status ==1||status==2) {
				news = newsService.selectAll(start,city);
				System.out.println(news);
			}else {
				news = newsService.selectBysex(start,status);
			}
			this.returnNickname(news);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return news;
	}
	
	
	//给每条动态设置昵称和头像
	public void returnNickname(List<News> news){
		User user = null;
		for(News n :news) {
			try {
				user = userService.selectByPrimaryKey(n.getUid());
				n.setUser(user);
				n.setComments(this.selectByNid(n.getId()));
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	//给每条动态设置评论的集合
	public List<Comment> selectByNid(Integer nid){
		List<Comment> comments =null;
		try {
			comments = commentService.selectByNid(nid);
			for(Comment c :comments) {
				c.setUser(userService.selectByPrimaryKey(c.getUid()));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return comments;
	}
	
	
	//根据动态的id返回一个news对象
	@GetMapping("/returnANews")
	public News returnANews(Integer id){
		List<News> news = new ArrayList<News>();
		try {
			News n = newsService.selectByPrimaryKey(id);
			news.add(n);
			this.returnNickname(news);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return news.get(0);
	}
	
	
	
	
}
